﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

public class DefineLoader : MonoBehaviour
{

    LuaEnv luaenv;

    void Start()
    {
        luaenv = new LuaEnv();
        luaenv.AddLoader(MyLoader);
        //luaenv.DoString("require '666'");

        luaenv.AddLoader(CoustmLoaderCallLua);

        luaenv.DoString("require 'example2'");
    }

    public byte[] MyLoader(ref string Path)
    {
        print(Path+"自定义loader被执行");
        return null;
    }
    /// <summary>
    /// 通过自定义loader 加载指定文件夹的lua文件
    /// </summary>
    /// <param name="path">路径</param>
    /// <returns></returns>
    public byte[] CoustmLoaderCallLua(ref string path)
    {
        Debug.Log(Application.streamingAssetsPath);
        string filePath = Application.streamingAssetsPath + "/" + path + ".lua.txt";
        string text= System.IO.File.ReadAllText(filePath);
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text);
        return bytes;
    }
    private void OnDestroy()
    {
        luaenv.Dispose();
    }
}
